package pe.com.tma.dao.jpa;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pe.com.tma.bean.CotizacionBean;
import pe.com.tma.dao.CotizacionDAO;
import pe.com.tma.model.Cotizacion;
import pe.com.tma.model.Instrumento;
import pe.com.tma.util.BusquedaGrl;
import pe.com.tma.util.RtnGeneral;

public class JPACotizacionDAO extends GenericDAOJpa<String, Cotizacion> implements CotizacionDAO {

	private EntityManager em = getEntityManager();
	private RtnGeneral rtn = null;
	@Override
	public List<Cotizacion> getAlzas() {
		try {
			Query q = em.createQuery("SELECT c FROM Cotizacion c WHERE c.varcot > 0 AND c.fecha = '2011-02-07'  ORDER BY c.varcot ASC");
			//q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
			return q.getResultList();
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	@Override
	public List<Cotizacion> getBajas() {
		try {
			Query q = em.createQuery("SELECT c FROM Cotizacion c WHERE c.varcot < 0 AND c.fecha = '2011-02-07' ORDER BY c.varcot DESC");
			//q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
			return q.getResultList();
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	@Override
	public List<Cotizacion> getMasNeg() {
		// TODO Auto-generated method stub
		try {
			Query q = em.createQuery("SELECT c FROM Cotizacion c WHERE c.fecha = '2011-02-07' AND c.montefe > 500000 ORDER BY c.montefe DESC");
			//q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
			return q.getResultList();
		} catch (Exception e) {
			// TODO: handle exception
			return null;
		}
	}
	@Override
	public Number obtenerUltCotizacion(Instrumento instrumento) {
		// TODO Auto-generated method stub
		Number rst = 0;
		try {
			Query q = em.createQuery("SELECT c.ultcot, MAX(c.fecha) FROM Cotizacion c WHERE c.codigo = ?1 ");
			q.setParameter(1, instrumento.getIdInstrumento());
			Iterator it = q.getResultList().iterator();
			
			while(it.hasNext()){
				Object[] par = ( Object[] ) it.next();
				rst = (Number) par[0];
				break;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		} finally{
			if(rst==null) rst=0;
		}
		System.out.println(rst.toString());
		return rst;
	}
	@Override
	public List<Cotizacion> obtenerHistorico(BusquedaGrl busc) {
		// TODO Auto-generated method stub
		try {
			Timestamp fecDesde = new Timestamp(busc.getFecDesde().getTime());
			Timestamp fecHasta = new Timestamp(busc.getFecHasta().getTime());
			
			Query q = em.createQuery("SELECT c FROM Cotizacion c WHERE c.codigo = ?1 AND  c.fecha BETWEEN ?2 and ?3 ORDER BY c.fecha ASC");
			//q.setHint("openjpa.hint.OptimizeResultCount", new Integer(2));
			q.setParameter(1, busc.getIdStr());
			q.setParameter(2, fecDesde);
			q.setParameter(3, fecHasta);
			return q.getResultList();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	
}
